package com.carvajal.adminpos.helpers;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.carvajal.adminpos.models.Commerce;
import com.carvajal.adminpos.models.Purchase;
import com.carvajal.adminpos.models.Reinstatement;
import com.carvajal.adminpos.models.User;

public class AdminPOSDbHelper extends SQLiteOpenHelper {
	
	// If you change the database schema, you must increment the database version.
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "ADMIN_POS.db";
    
    /**
     * 
     * @param context
     */
    public AdminPOSDbHelper(Context context) {
    	super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    /*
     * (non-Javadoc)
     * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
     */
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(User.SQL_CREATE_ENTRIES);
		db.execSQL(Purchase.SQL_CREATE_ENTRIES_PURCHASE);
		db.execSQL(Commerce.SQL_CREATE_ENTRIES_COMMERCE);
		db.execSQL(Reinstatement.SQL_CREATE_ENTRIES);
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL(User.SQL_DELETE_ENTRIES);
		db.execSQL(Purchase.SQL_DELETE_ENTRIES_PURCHASE);
		db.execSQL(Commerce.SQL_DELETE_ENTRIES_COMMERCE);
		db.execSQL(Reinstatement.SQL_DELETE_ENTRIES);
        onCreate(db);

	}
	
	/*
	 * (non-Javadoc)
	 * @see android.database.sqlite.SQLiteOpenHelper#onDowngrade(android.database.sqlite.SQLiteDatabase, int, int)
	 */
	@Override
	public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
}
